package elshopscanner;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class CachePageLoader {
	static String FName = "D:/elshopcche.zip"; 
	public CachePageLoader() {
		// TODO Auto-generated constructor stub
	}
	
	void SaveToZip(String url, String Document) throws Exception {
		File F = new File(FName);
		File tempFile = File.createTempFile(F.getName(), null);
		tempFile.delete();

		File zipFile = new File(FName);

		boolean renameOk = zipFile.renameTo(tempFile);
//		if (!renameOk) {
//			throw new RuntimeException("could not rename the file "
//					+ zipFile.getAbsolutePath() + " to "
//					+ tempFile.getAbsolutePath());
//		}
		byte[] buf = new byte[1024];


		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

		if (renameOk) {
			ZipInputStream zin = new ZipInputStream(new FileInputStream(
					tempFile));
			ZipEntry entry = zin.getNextEntry();
			while (entry != null) {
				String name = entry.getName();
				out.putNextEntry(new ZipEntry(name));
				int len;
				while ((len = zin.read(buf)) > 0) {
					out.write(buf, 0, len);
				}
				// }
				entry = zin.getNextEntry();
			}
			// Close the streams
			zin.close();
		}
		// Compress the files

		String zipFName = url.replaceAll("/", "_");

		ZipEntry e = new ZipEntry(zipFName);
		out.putNextEntry(e);

		byte[] data = Document.getBytes("UTF-8");
		out.write(data, 0, data.length);
		out.closeEntry();

		// Complete the ZIP file
		out.close();
		tempFile.delete();

	}

	String ReadFromZip(String url) throws Exception{
		File F = new File(FName);
		if (!F.exists()) return null;
		String zipFName = url.replaceAll("/", "_");
		ZipFile ZF = new ZipFile(F);
		ZipEntry ZE = ZF.getEntry(zipFName);
		if (ZE==null) return null;
		byte[] data=new byte[(int)ZE.getSize()];
		InputStream ZIS = ZF.getInputStream(ZE);
		//if (
		int Off=0;
		while (Off<data.length) {
			System.out.println(Off);
			Off+=ZIS.read(data, Off, data.length-Off);
		}
	    String Doc = new String(data, "UTF-8"); 
		return Doc;
	}
	
	public Document LoadPage(String name, int page) throws Exception {	
    	Connection conn = Jsoup.connect("http://www.kosmodrom.com.ua/prodlist.php");
    	conn.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
    	conn.followRedirects(true);
    	conn.timeout(10000);
    	conn.data("name", name);
    	conn.data("page", page+"");
    	conn.referrer("www.kosmodrom.com.ua");
    	conn.header("Accept", "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    	conn.header("Accept-Charset", "windows-1251,utf-8;q=0.7,*;q=0.3");
    	conn.header("Accept-Encoding", "gzip,deflate,sdch");
    	conn.header("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4");
    	
    	Document doc; 		
 
		String sDoc = ReadFromZip(name+"_"+page);
		if (sDoc!=null) {doc=Jsoup.parse(sDoc); }
		else {
			while (true) {
				try {
					doc = conn.get();
					break;
				} catch (Exception Ex) {
					Thread.sleep(1000);
					System.out.println("Connection error. try again");
				}
			}
			SaveToZip(name+"_"+page, doc.toString());

		}
		return doc;
	}
}
